package cn.tedu.weibo.controller;

import cn.tedu.weibo.pojo.dto.WeiboDTO;
import cn.tedu.weibo.pojo.vo.WeiboDetailVO;
import cn.tedu.weibo.pojo.vo.WeiboIndexVO;
import cn.tedu.weibo.response.JsonResult;
import cn.tedu.weibo.service.WeiboService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("v1/weibos")
public class WeiboController {

    @Autowired
    WeiboService weiboService;

    @PostMapping("add-new")
    public JsonResult addNew(WeiboDTO weiboDTO) {
        System.out.println("weiboDTO = " + weiboDTO);
        weiboService.addNew(weiboDTO);
        return JsonResult.ok();
    }

    @GetMapping("list")
    public JsonResult listForIndex() {
        List<WeiboIndexVO> list = weiboService.listForIndex();
        return JsonResult.ok(list);
    }
    @GetMapping("")/*用于应对'http://localhost:8080/v1/weibos?userId'*/
    public JsonResult listForIndexByUserId(Long userId){
        List<WeiboIndexVO> list = weiboService.listForIndexByUserid(userId);
        System.out.println("userId = " + userId);
        System.out.println(list);
        return JsonResult.ok(list);
    }
    @PostMapping("{id}/delete")
    public JsonResult deleteByWeiboId(@PathVariable Long id){
        weiboService.deleteByWeiboId(id);
        return JsonResult.ok();
    }
    @GetMapping("{id}/detail")
    public JsonResult detailById(@PathVariable Long id){
        WeiboDetailVO weiboDetailVO = weiboService.detailById(id);
        return JsonResult.ok(weiboDetailVO);
    }

}
